package com.xinlan.imageeditlibrary.editimage.adapter;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xinlan.imageeditlibrary.R;
import com.xinlan.imageeditlibrary.editimage.fliter.PhotoProcessing;
import com.xinlan.imageeditlibrary.editimage.view.ZQImageViewRoundOval;

import java.util.ArrayList;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/9/13 10:59
 */
public class LvJingAdapter extends RecyclerView.Adapter<LvJingAdapter.LvJingHolder> {
    private String[] fliterName;
    private String[] flie;
    private Activity activity;
    private Bitmap mOldBitmap;
    private LayoutInflater mInflater;

    private OnLvJingClickListener mOnLvJingClickListener;
    private Drawable mSelectDrawable;
    private ArrayList<Integer> mSelectList = new ArrayList<>();
    private ZQImageViewRoundOval mLastPicture;
    private LinearLayout mLastLlShape;

    public void setOnLvJingClickListener(OnLvJingClickListener onLvJingClickListener) {
        this.mOnLvJingClickListener = onLvJingClickListener;
    }

    public LvJingAdapter(String[] fliterName, String[] flie, LayoutInflater mInflater, Activity activity) {
        this.fliterName = fliterName;
        this.flie = flie;
        this.mInflater = mInflater;
        this.activity = activity;
        mSelectDrawable = activity.getResources().getDrawable(R.drawable.shape_img_filter_select);
    }

    public void setOldBitmap(Bitmap oldBitmap) {
        this.mOldBitmap = oldBitmap;
        notifyDataSetChanged();
    }

    public void clearSelectList() {
        this.mSelectList.clear();
        if (mLastPicture != null) {
            mLastPicture.setAlpha(1f);
            mLastLlShape.setBackground(null);
            mLastLlShape = null;
            mLastPicture = null;
        }
    }

    @NonNull
    @Override
    public LvJingHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new LvJingHolder(mInflater.inflate(R.layout.item_lvjing, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final LvJingHolder lvJingHolder, final int i) {
        try {
            lvJingHolder.mIvItemlvPicture.setRoundRadius(5);
            if (i == 0) {
                lvJingHolder.mIvItemlvPicture.setImageBitmap(mOldBitmap);
            } else {
                ProcessingImage task = new ProcessingImage(mOldBitmap, lvJingHolder.mIvItemlvPicture);
                task.execute(i);
            }
            if (mSelectList.size() > 0) {
                if (mSelectList.contains(i)) {
                    lvJingHolder.mLlShape.setBackground(mSelectDrawable);
                    lvJingHolder.mIvItemlvPicture.setAlpha(0.2f);
                    mLastLlShape = lvJingHolder.mLlShape;
                    mLastPicture = lvJingHolder.mIvItemlvPicture;
                } else {
                    lvJingHolder.mLlShape.setBackground(null);
                    lvJingHolder.mIvItemlvPicture.setAlpha(1f);
                }
            } else {
                if (i == 0) {
                    lvJingHolder.mIvItemlvPicture.setAlpha(0.2f);
                    lvJingHolder.mLlShape.setBackground(mSelectDrawable);
                    mLastLlShape = lvJingHolder.mLlShape;
                    mLastPicture = lvJingHolder.mIvItemlvPicture;
                } else {
                    lvJingHolder.mIvItemlvPicture.setAlpha(1f);
                    lvJingHolder.mLlShape.setBackground(null);
                }
            }
            lvJingHolder.mTvItemlvFunction.setText(fliterName[i]);
            lvJingHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mSelectList.clear();
                    mSelectList.add(i);
                    if (mLastPicture != null) {
                        mLastPicture.setAlpha(1f);
                        mLastLlShape.setBackground(null);
                    }
                    mLastLlShape = lvJingHolder.mLlShape;
                    mLastPicture = lvJingHolder.mIvItemlvPicture;
                    lvJingHolder.mLlShape.setBackground(mSelectDrawable);
                    lvJingHolder.mIvItemlvPicture.setAlpha(0.2f);
                    if (mOnLvJingClickListener != null)
                        mOnLvJingClickListener.onLvJingClick(i);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return fliterName.length;
    }


    public class LvJingHolder extends RecyclerView.ViewHolder {
        private ZQImageViewRoundOval mIvItemlvPicture;
        private TextView mTvItemlvFunction;
        private LinearLayout mLlShape;

        public LvJingHolder(@NonNull View itemView) {
            super(itemView);

            mLlShape = itemView.findViewById(R.id.ll_shape);
            mIvItemlvPicture = itemView.findViewById(R.id.iv_itemlv_picture);
            mTvItemlvFunction = itemView.findViewById(R.id.tv_itemlv_function);
            mIvItemlvPicture.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

    public interface OnLvJingClickListener {
        void onLvJingClick(int pos);
    }


    /**
     * 图片滤镜处理任务
     *
     * @author panyi
     */
    private final class ProcessingImage extends AsyncTask<Integer, Void, Bitmap> {
        private Bitmap oldBitmap;
        private Bitmap srcBitmap;
        private ImageView picture;

        public ProcessingImage(Bitmap oldBitmap, ImageView mIvItemlvPicture) {
            this.oldBitmap = oldBitmap;
            this.picture = mIvItemlvPicture;
        }

        @Override
        protected Bitmap doInBackground(Integer... params) {
            int type = params[0];
            if (srcBitmap != null && !srcBitmap.isRecycled()) {
                srcBitmap.recycle();
            }

            srcBitmap = Bitmap.createBitmap(oldBitmap.copy(
                    Bitmap.Config.ARGB_8888, true));
            return PhotoProcessing.filterPhoto(srcBitmap, type);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        protected void onCancelled(Bitmap result) {
            super.onCancelled(result);
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            if (result == null)
                return;
            picture.setImageBitmap(result);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

    }// end inner class

}
